/**
 * 初始化table组建信息  数据处理
 */
layui.define(['table', 'form'], function (exports) {
    "use strict";

    var table = layui.table, form = layui.form;
    table.set({
        page: true,
        limit: 10,
        limits: [5, 10, 20, 30, 50],
        even: true,
        method: 'POST',
        autoSort: false,
        text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        },
        request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        response: {
            // statusName: 'success' //数据状态的字段名称，默认：code 加上有问题
            statusCode: 0 //成功的状态码，默认：0
            , msgName: 'message' //状态信息的字段名称，默认：msg
            , countName: 'total' //数据总数的字段名称，默认：count
            , dataName: 'data' //数据列表的字段名称，默认：data
        },
        beforeHandle: function (params) {
            var NOT_DECRYPT_PARAM_NAMES = ['_csrf', 'page', 'pageSize', 'geetest_challenge', 'geetest_validate', 'geetest_seccode'];
            for (var key in params) {
                if (NOT_DECRYPT_PARAM_NAMES.indexOf(key) >= 0) continue;
                params[key] = window.base64.encode(params[key] != undefined && params[key] != null ? params[key].toString() : "", 2);
            }
            return params;
        }
    });

    //------- tablePlug --------
    // 缓存当前操作的是哪个表格的哪个tr的哪个td
    $(document).off('mousedown', '.layui-table-grid-down')
        .on('mousedown', '.layui-table-grid-down', function (event) {
            // 记录操作的td的jquery对象
            table._tableTdCurr = $(this).closest('td');
        });

    // 给弹出的详情里面的按钮添加监听级联的触发原始table的按钮的点击事件
    $(document).off('click', '.layui-table-tips-main [lay-event]')
        .on('click', '.layui-table-tips-main [lay-event]', function (event) {
            var elem = $(this);
            var tableTrCurr = table._tableTdCurr;
            if (!tableTrCurr) {
                return;
            }
            var layerIndex = elem.closest('.layui-table-tips').attr('times');
            // 关闭当前的这个显示更多的tip
            layer.close(layerIndex);
            // 找到记录的当前操作的那个按钮
            table._tableTdCurr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
        });

    // 监听统一的toolbar一般用来处理通用的
    table.on('toolbar()', function (obj) {
        var config = obj.config;
        var btnElem = $(this);
        var tableId = config.id;
        var tableView = config.elem.next();
        switch (obj.event) {
            case 'LAYTABLE_COLS':
                // 给筛选列添加全选还有反选的功能
                var panelElem = btnElem.find('.layui-table-tool-panel');
                var checkboxElem = panelElem.find('[lay-filter="LAY_TABLE_TOOL_COLS"]');
                var checkboxCheckedElem = panelElem.find('[lay-filter="LAY_TABLE_TOOL_COLS"]:checked');
                $('<li class="layui-form" lay-filter="LAY_TABLE_TOOL_COLS_FORM">' +
                    '<input type="checkbox" lay-skin="primary" lay-filter="LAY_TABLE_TOOL_COLS_ALL" '
                    + ((checkboxElem.length === checkboxCheckedElem.length) ? 'checked' : '') + ' title="全选">' +
                    '<span class="LAY_TABLE_TOOL_COLS_Invert_Selection">反选</span></li>')
                    .insertBefore(panelElem.find('li').first())
                    .on('click', '.LAY_TABLE_TOOL_COLS_Invert_Selection', function (event) {
                        layui.stope(event);
                        // 反选逻辑
                        panelElem.find('[lay-filter="LAY_TABLE_TOOL_COLS"]+').click();
                    });
                form.render('checkbox', 'LAY_TABLE_TOOL_COLS_FORM');
                break;
        }
    });

    // 监听筛选列panel中的全选
    form.on('checkbox(LAY_TABLE_TOOL_COLS_ALL)', function (obj) {
        $(obj.elem).closest('ul')
            .find('[lay-filter="LAY_TABLE_TOOL_COLS"]' + (obj.elem.checked ? ':not(:checked)' : ':checked') + '+').click();
    });

    // 监听筛选列panel中的单个记录的change
    $(document).on('change', 'input[lay-filter="LAY_TABLE_TOOL_COLS"]', function (event) {
        var elemCurr = $(this);
        // 筛选列单个点击的时候同步全选的状态
        $('input[lay-filter="LAY_TABLE_TOOL_COLS_ALL"]')
            .prop('checked',
                elemCurr.prop('checked') ? (!$('input[lay-filter="LAY_TABLE_TOOL_COLS"]').not(':checked').length) : false);
        form.render('checkbox', 'LAY_TABLE_TOOL_COLS_FORM');
    });

    // 让被美化的复选框支持原始节点的change事件
    form.on('checkbox', function (data) {
        $(data.elem).change();
    });

    layui.link(layui.cache.base + 'initTable/initTable.css?v=' + (layui.cache.version ? layui.cache.version : '1'));
    exports('initTable', {});
});